// electron-main/index.ts
import { app, BrowserWindow, ipcMain, Menu, screen, globalShortcut } from "electron";
import { release } from 'os'
import { join } from "path";
import pkg from '../../package.json'

if (release().startsWith('6.1')) app.disableHardwareAcceleration()
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
if (!app.requestSingleInstanceLock()) {
  app.quit()
  process.exit(0)
}

export const ROOT_PATH = {
  dist: join(__dirname, '../..'),
  public: join(__dirname, app.isPackaged ? '../..' : '../../../public'),
}

const preload = join(__dirname, '../preload/index.js')
const url = `http://${pkg.env.VITE_DEV_SERVER_HOST}:${pkg.env.VITE_DEV_SERVER_PORT}`
const indexHtml = join(ROOT_PATH.dist, 'index.html')
let win

const createWindow = () => {
  win = new BrowserWindow({
    icon: join(ROOT_PATH.public, 'favicon.ico'),
    width: 1300 + 50,
    height: 800 + 50,
    resizable: false,
    transparent: true,
    autoHideMenuBar: true, // 隐藏menu
    webPreferences: {
      preload,
      webSecurity: false, //解决跨域
      nodeIntegration: true,
      contextIsolation: false,
    },
  });

  const menuTemp = [
    {
      label: '功能选项',
      submenu: [
        {
          label: '控制台',
          accelerator: 'ctrl+i',
          click: () => {
            win.webContents.openDevTools()
          },
        },
        {
          label: '刷新',
          accelerator: 'ctrl+r',
          click: () => {
            win.reload();
          },
        }
      ]
    }
  ]

  const menu = Menu.buildFromTemplate(menuTemp as any)
  Menu.setApplicationMenu(menu)
  // 如果打包了，渲染index.html
  if (app.isPackaged) {
    win.loadFile(indexHtml)
  } else {
    win.loadURL(url);
  }
};

app.whenReady().then(() => {
  globalShortcut.register('F11', () => {
    return false;
  })
}).then(() => {
  createWindow(); // 创建窗口
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
})

// 关闭窗口
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});


ipcMain.on('open-Url', (event, arg) => {
  const width = screen.getPrimaryDisplay().size.width
  const height = screen.getPrimaryDisplay().size.height
  win.setContentSize(width, height); // 重新设置大小
  win.setFullScreen(true);
  win.setSimpleFullScreen(true);
})

ipcMain.on('back', (event, arg) => {
  win.setFullScreen(false);
  win.setSimpleFullScreen(false);
  win.setContentSize(1300 + 50, 800 + 50,); // 重新设置大小
  win.center() // 居中显示
})

ipcMain.on('minimizes', (event, arg) => {
  // win.minimize()
  win.hide()
})

ipcMain.on('backLogin', (event, arg) => {
  win.setFullScreen(false);
  win.setSimpleFullScreen(false);
  win.setContentSize(684 + 50, 484 + 50,); // 重新设置大小
  win.center() // 居中显示
})

// 关闭应用(一次)
ipcMain.once('close', (event, arg) => {
  app.quit();
});

// 应用启动
ipcMain.handle('open-win', (event, arg) => {
  const childWindow = new BrowserWindow({
    webPreferences: {
      preload,
      nodeIntegration: true,
    },
  })
  if (app.isPackaged) {
    childWindow.loadFile(indexHtml, { hash: arg })
  } else {
    childWindow.loadURL(`${url}/#${arg}`)
  }
})
